package com.mingqijia.gassafety.authority.dal.handler;

import com.mingqijia.gassafety.authority.api.req.company.CreateCompanyReqDTO;
import com.mingqijia.gassafety.authority.api.req.company.QueryCompanyReqDTO;
import com.mingqijia.gassafety.authority.api.req.company.UpdateCompanyReqDTO;
import com.mingqijia.gassafety.authority.api.resp.company.CompanyLiteRespDTO;
import com.mingqijia.gassafety.authority.api.resp.company.CompanyRespDTO;
import com.mingqijia.gassafety.authority.dal.command.CreateCompanyCommand;
import com.mingqijia.gassafety.authority.dal.command.QueryCompanyCommand;
import com.mingqijia.gassafety.authority.dal.command.RemoveCompanyCommand;
import com.mingqijia.gassafety.authority.dal.command.UpdateCompanyCommand;
import com.mingqijia.gassafety.authority.dal.handler.CompanyTranslatorImpl;
import com.mingqijia.gassafety.db.entity.auth.CompanyEntry;
import com.mingqijia.gassafety.shared.utils.MappingTools;
import org.mapstruct.Mapper;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;

import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 对象转换接口
 * @author Damon S.
 * @version v1.0.1
 * @date 2020年01月09日 20:37
 */
@Mapper(componentModel = "spring", uses = MappingTools.class)
@ConditionalOnMissingBean(CompanyTranslatorImpl.class)
public interface CompanyTranslator {

    CompanyLiteRespDTO toLiteRespDTO(CompanyEntry entry);

    CompanyRespDTO toRespDTO(CompanyEntry entry);

    QueryCompanyCommand toCommand(QueryCompanyReqDTO req);

    CreateCompanyCommand toCommand(CreateCompanyReqDTO req);

    UpdateCompanyCommand toCommand(UpdateCompanyReqDTO req);

    CompanyEntry toEntry(CreateCompanyCommand command);

    CompanyEntry toEntry(UpdateCompanyCommand command);

    CompanyEntry toEntry(RemoveCompanyCommand command);


    default List<CompanyRespDTO> toRespDTO(List<CompanyEntry> entries) {
        return Optional.ofNullable(entries)
                .orElse(Collections.emptyList())
                .stream().map(this::toRespDTO)
                .collect(Collectors.toList());
    }

    default List<CompanyLiteRespDTO> toLiteRespDTO(List<CompanyEntry> entries) {
        return Optional.ofNullable(entries)
                .orElse(Collections.emptyList())
                .stream().map(this::toLiteRespDTO)
                .collect(Collectors.toList());
    }
}
